| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import NextAuth from 'next-auth';
- import Credentials from 'next-auth/providers/credentials';
-
- import { connectToDatabase } from '../../../utils/helpers/dbHelpers';
- import { verifyPassword } from '../../../utils/helpers/hashPasswordHelpers';
-
- export default NextAuth({
- session: {
- jwt: true,
- },
- providers: [
- Credentials({
- async authorize(credentials) {
- const client = await connectToDatabase();
-
- const usersCollection = client.db().collection('users');
-
- const user = await usersCollection.findOne({
- username: credentials.username,
- });
-
- if (!user) {
- client.close();
- throw new Error('No user found!');
- }
-
- const isValid = await verifyPassword(
- credentials.password,
- user.password
- );
-
- if (!isValid) {
- client.close();
- throw new Error('Could not log you in!');
- }
-
- client.close();
- return { name: user.fullName };
- },
- }),
- ],
- });
|